// pages/orderDetails/orderDetails.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if(options.q!==undefined){
      var rest = decodeURIComponent(options.q);
      if (rest.indexOf("code") < 0 || rest.indexOf("type") < 0) {
        util.showStatus("请扫描核销二维码");
        return;
      }
      var arr = res.result.split("?");
      var str1 = arr[1];
      var atrr1 = str1.split("&")[0];
      var code = atrr1.split("=")[1];
      var types = str1.split("&")[1].split("=")[1];
      that.setApiCouponsCodeDetails(code, types);
    }else{
      that.setData({
        oid: options.oid
      })
      util.showLoading("加载中...");
      setTimeout(function(){
        that.orderDetails();
      },300)
    }
  },

  //请求后台，改变订单状态为已扫码
  setApiCouponsCodeDetails: function (couponsCode, couponsType) {
    var that=this;
    var params = {
      code: couponsCode,
      type: couponsType
    }
    util.showLoading("加载中...");
    api.POST({
      url: app.globalData.base_url + 'shop/scanInfo',
      params: params,
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
              oid: res.data.data.oid
          })
          setTimeout(function () {
            that.orderDetails();
          }, 300)
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (res) {
        wx.hideLoading();
        util.showError("网络错误，请检查网络。");
      }
    });
  },
  
  //订单详情
  orderDetails: function () {
    var that = this;
    var oid = this.data.oid;
    api.POST({
      params: { "oid": oid },
      url: app.globalData.base_url + 'shop/getOrderInfo',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var orderInfo = res.data.data;
          var productList = res.data.data.productList;
          var newArr = [];
          var pickupCode = orderInfo.pickup_code;
          for (var i = 0; i < pickupCode.length; i++) {
            var item = pickupCode.substr(i, 1);
            newArr.push(item)
          }
          var numbers = 0;
          for (var i = 0; i < productList.length; i++) {
            numbers = numbers / 1 + productList[i].number / 1;
          }
          that.setData({
            orderInfo: orderInfo,
            newArr: newArr,
            numbers: numbers
          })
        }
      },
      fail: function (fail) {
        wx.hideLoading();
        util.showError("网络错误，请检查网络。");
      }
    })
  },

  //取消提货
  cancelClaimGoods: function (e) {
    var that = this;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({ lastTime: curTime })
    if ((lastTime > 0) && (curTime - lastTime < 1500)) {
      return false;//禁止连续点击
    }
    wx.showModal({
      title: '提示',
      content: '确定取消提货',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { oid: that.data.oid },
            url: app.globalData.base_url + 'shop/cancelPickUp',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("网络错误，请检查网络。");
            }
          })
        } 
      }
    })
  },

  //打开柜门
  scan: function (e) {
    var that = this;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({ lastTime: curTime })
    if ((lastTime > 0) && (curTime - lastTime < 1500)) {
      return false;//禁止连续点击
    }
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        var rest = res.result;
        if (rest.indexOf("machineid") < 0) {
          util.showStatus("请扫描机器二维码");
          return;
        }
        var arr = res.result.split("?");
        var str1 = arr[1];
        var atrr1 = str1.split("&")[0];
        var machine_id = atrr1.split("=")[1];
        that.setData({
          machine_id: machine_id
        })
        wx.redirectTo({
          url: '../openDoor/openDoor?machine_id=' + machine_id + '&oid=' + that.data.oid,
        })
      }
    })
  },
})